<script src="/js/functions.js"></script>
<script src="/js/utils.js"></script>
<script src="/js/dump.js"></script>
<script>

var trace = function(x) {
	document.write('<pre>' + sprintf.apply(this, arguments) + '</pre>');
};


var Stream = function(str) {
	this.load(str);
};
Stream.prototype = {
	EOF: -1,
	load: function(str) {
		this.source = str;
		this.length = this.source.length;
		this.reset();
	},
	eof: function() {
		return this.char === this.EOF;
	},
	reset: function() {
		this.pos = 0;
	},
	getch: function() {
		if (this.pos >= this.length)
			return this.char = this.EOF;
		return this.char = this.source[this.pos++];
	}
};


var parse_re = function(source) {
	var rec = function(stream, result) {
		var c = stream.getch();
		switch (c) {
			case stream.EOF:
				return result;
			case '|':
				var x = rec(stream, []);
				return  ['|', result, x];
			case '(':
				var x = rec(stream, []);
				result.push(x);
				return rec(stream, result);
			case ')':
				return result.length == 1 ? result[0] : result;
			case '?':
				result.push(['?', result.pop()]);
				return rec(stream, result);
			default:
				result.push(['C', c]);
				//result.push(c);
				return rec(stream, result);
		}
	};
	var stream = new Stream(source);
	return rec(stream, []);
};

var x = parse_re('(xy)?');
console.dir(x);
trace(jsonrpc_json_encode(x));




</script>















